﻿using BenCaoXuan.Infrastructure.Attr;
using BenCaoXuan.Infrastructure.Dto;
using BenCaoXuan.Infrastructure.Dto.Admin.employee;
using BenCaoXuan.Infrastructure.EFCore.MySql;
using BenCaoXuan.Infrastructure.Tools;
using BenCaoXuan.Service.Interface.admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace BenCaoXuan.Api.Admin.Controllers.admin
{
    [ApiGroup("Admin(管理端模块)")]
    public class AdminController : BaseController
    {
        private readonly IConfiguration _configuration;
       
        public AdminController(IConfiguration configuration,I_Login_Service login_Service):base(login_Service)
        {
            _configuration = configuration;
           
        }

        

        /// <summary>
        /// 管理员-登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public  async Task<IActionResult> Logins(Login_Dto dto)
        {
            dto.code = Config.GUID2();
            var data = await _login_Service.Logins(dto);
            if (data.code == Api_Code.ok)
            {
                var claims = new[]
                    {
                    new Claim(ClaimTypes.Name,  dto.code),
                    new Claim(ClaimTypes.Actor,  dto.account!),
                    new Claim(ClaimTypes.Role,"admin")//角色设定
                };
                var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:SecurityKey"]));
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                string jwtKey = _configuration["JWT:issuer"];
                var token = new JwtSecurityToken(
                    issuer: jwtKey,
                    audience: jwtKey,
                    claims: claims,
                    expires: DateTime.Now.AddMinutes(60 * 12), //token 过期时间
                    signingCredentials: creds);

                data.data = new
                {
                    token = new JwtSecurityTokenHandler().WriteToken(token)
                };

            }

            return Ok(data);
        }

        /// <summary>
        /// 初始化员工密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Init_User()
        {
            var result = await _login_Service.Init_User();
            return Ok(result);
        }


      

        /// <summary>
        /// 检查登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "admin")]
        public IActionResult Check_login() 
        {
            var employee = this.Get_AdminUser();
            if (employee == null)
            {
                return Unauthorized();//401
            }
            return Ok(new Api_Result_Dto()
            {
                code = 0,
                message = "ok",
                data = employee
            });
        }


    }
}
